package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Transactional
public class UserController {
    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送验证码
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String, String> map) {
        //1.接收请求参数
        String phone = map.get("phone");
        //2.随机生成验证码 todo 临时写死给老郭省点钱
        //String code = RandomUtil.randomNumbers(6);
        String code = "666666";
        log.info("生成的验证码是:{}", code);
        //3.发送验证码 todo 临时写死
        //smsTemplate.sendSms(phone, code);
        //4.记录验证码，方便后期比较
        //session.setAttribute(Constant.USER_PHONE_CODE + phone, code);
        //=======================调整1: 发送短信之后,需要将验证码存储到redis,而且要设置5分钟有效时间=========================//
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("USER_PHONE_CODE:" + phone,code,5, TimeUnit.MINUTES);
        //5.返回结果
        return ResultInfo.success(null);
    }

    //登录/注册
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");
        String code = map.get("code");
        //2. 对比验证码
//        String codeFromSession = (String) session.getAttribute(Constant.USER_PHONE_CODE + phone);
//        if (StringUtils.isEmpty(codeFromSession) || !StringUtils.equals(code, codeFromSession)) {
//            return ResultInfo.error("验证码错误，请重新输入");
//        }


        //=======================调整2: 以后在登录的时候,比对验证码就应该比对前台的和redis里面取出的=========================//
        String codeFromRedis = (String) redisTemplate.opsForValue().get("USER_PHONE_CODE:" + phone);
        if (StringUtils.isEmpty(codeFromRedis)||!StringUtils.equals(code,codeFromRedis)){
            return ResultInfo.error("验证码错误，请重新输入");
        }

        //3. 查询用户是否在数据库中
        User user = userService.findByPhone(phone);
        if (user != null) {
            if (user.getStatus() == 0) {
                return ResultInfo.error("用户被禁用，请联系管理员");
            }
        } else {
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        //4.用户信息放入session
        session.setAttribute(Constant.SESSION_USER, user);
        //5. 返回成功信息
        return ResultInfo.success(null);

    }


}
